package kfm.util;

import java.util.Arrays;

public class List {
    private Object[] arr;
    private int size;
    public List(int size){
        arr = new  Object[size];
    }

    public  boolean add(Object object){
        if (size == arr.length ){
            grow();
        }
        arr[size ++] = object;
        return true;
    }

    private void grow() {
        int newLength = (arr.length + 10);
        arr = Arrays.copyOf(arr,newLength);
    }

    public int size(){
        return size;
    }

    public int search(Object object){
        for (int i = 0; i < size; i++) {
            if (arr[i].equals(object)){
                return i;
            }
        }
        return -1;
    }
    /**
     * 获取元素
     * @param index
     * @return
     */
    public Object getElment(int index){
        if(index < 0 || index > size - 1){
            return null;
        }else{
            return arr[index];
        }
    }



    public String toString(){
        String str = "";
        for (int i = 0; i < size; i++) {
            str += arr[i].toString() + "\n";
        }
        return str;
    }

}
